home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-05-21 | 7.4 KB | 236 lines | [TEXT/ttxt] |
- -- <<<
- -- Filename:
- -- ads.sx
- --
- -- Other Files Required:
- -- adobject.sx
- --
- -- Purpose:
- -- Define the ClassifiedAds document and data objects
- --
- -- Specialized Classes:
- -- ClassifiedAds - adds the following instance variables
- -- adsPerPage: an integer, the number of ads per page. This is
- -- required to calculate the number of pages
- -- based on the number of data objects
- -- adPageTemplate: an instance of the built-in PageLayer class. This is
- -- required when we create a new page.
- -- adProtocol: an instance of whatever class is used to contain
- -- the data for the classified ads. This is used to
- -- test if objects added to the classifieds are of the
- -- expected type. It is also used as a "blank ad", to
- -- fill out pages that don't have a full complement of ads.
- -- It also includes four methods:
- -- addClassified self adObject
- -- Appends the adObject to the list of data objects. If there are
- -- not enough pages to display all of the data objects, a new page
- -- is created.
- -- removeClassified self adObject
- -- Removes the adObject from the list of data objects. If the
- -- removal makes the last page empty, that empty page is deleted.
- -- appendNewClassifiedPage self
- -- (Private) Appends a new page to the document.
- -- pageData self #key pageNum
- -- (Private) Returns the list of data objects which map to the
- -- given pageNum. Invoked (eventually) by the data functions of
- -- the page elements in the adPageTemplate.
- -- Author:
- -- Steve Gano, Dionn M. Stewart, Felicia Santelli
-
- in module Autofinder
-
- class ClassifiedAds (Document)
- instance variables
- adsPerPage -- integer, number of ads per page
- adPageTemplate -- PageLayer object
- adProtocol -- single object of class acceptable for ad data
- boundary -- work around oneofnpresenter inheritance
- pageData -- data for that page
- media
- pageLeft -- button for changing pages
- pageRight -- button for changing pages
- control
- end
-
- method init self {class ClassifiedAds} #rest args \
- #key media: (undefined) \
- adsPerPage:(3) \
- name:("Classified Ads") \
- boundary: (new rect) ->
- (
- apply nextMethod self args
-
- -- We should test here for presence of required key args
- self.adsPerPage := adsPerPage
- self.boundary := boundary
- self.pageData := #()
- self.name := name
- self.media := media
- self
- )
-
- method afterinit self {class ClassifiedAds} #rest args \
- #key adPageTemplate:(undefined) adProtocol:(undefined) ->
- (
- local media := self.media
-
- self.adPageTemplate := new ThreeAdPageTemplate \
- boundary:self.boundary data:undefined media:media
- self.adProtocol := new adDataObject
-
- --Make the pushbutton elements to turn the pages
- self.pageLeft:= new pushButton pressedPresenter:media["left button"]\
- releasedPresenter:(new twodshape boundary:media["left button"].bbox)\
- disabledPresenter:media["left button"]
- self.pageLeft.x := 13; self.pageLeft.y := 18
- self.pageLeft.authordata := self
- self.pageLeft.activateAction := (adata btn -> (updatePagebuttons adata @backward))
- prepend self.adPageTemplate self.pageLeft
-
- self.pageRight:= new pushButton pressedpresenter:media["right button"]\
- releasedPresenter:(new twodshape boundary:media["right button"].bbox)\
- disabledpresenter:media["right button"]
- self.pageRight.x := 577; self.pageRight.y := 18
- self.pageRight.authordata := self
- self.pageRight.activateAction:= (adata btn -> (updatePagebuttons adata @forward))
- prepend self.adPageTemplate self.pageRight
-
- local ac:= new actuatorcontroller space:self.adPageTemplate
- ac.wholespace:= true
- self.control := #(ac)
-
- -- Create a blank page
- appendNewClassifiedPage self
-
- self
- )
-
- method leaveScene self {class ClassifiedAds} ->
- (
- for i in self.control do
- i.space := undefined
- emptyout self.control
-
- for i in self.data do
- makepurgeable i.adpic
-
- emptyOut self
- )
-
- method addClassified self {class ClassifiedAds} adObject ->
- (
- -- Make sure the ad object is of the type we accept.
- if (getclass adObject) <> (getClass self.adProtocol) do return
-
- -- If the ad is already in our list, just return
- if (ismember self.data adObject) do return
-
- append self.data adObject
-
- -- If all of the pages are full, add a new page for this object.
- if trunc (((size self.data - 1)/ self.adsPerPage) + 1) > size self do
- (
- appendNewClassifiedPage self
- )
- )
-
- method removeClassified self {class ClassifiedAds} adObject ->
- (
- -- If the ad object is not currently on our list, just return
- if not (ismember self.data adObject) do return
-
- -- Delete the object from the data list.
- deleteOne self.data adObject
-
- -- If this made an empty page, delete it (unless it's the only page)
- if ((((size self.data - 1)/ self.adsPerPage) + 1) < size self) and
- (size self > 1) do
- (
- deleteOne self (getnth self (size self))
- )
- )
-
- method appendNewClassifiedPage self {class ClassifiedAds} ->
- (
- -- Append a new page, using the adPageTemplate page layer.
- -- Use our pageData method as the way to derive the data for the page.
- append self (new page boundary:self.boundary frame:self.adPageTemplate \
- data:(p -> pageData p.presentedBy) )
- )
-
- method pageData self {class ClassifiedAds} #key pageNum:(self.cursor) ->
- (
- -- Return a list of the ad data objects for the given page.
- local firstObjNdx := max 1 (((pageNum - 1) * self.adsPerPage) + 1)
- local lastObjNdx := min (firstObjNdx + 2) (size self.data)
-
- -- In some cases (e.g., no ads), firstNdx will be greater than lastNdx
- local ads :=
- if firstObjNdx > lastObjNdx then
- #()
- else
- for objNdx := firstObjNdx to lastObjNdx collect
- (getnth self.data objNdx)
-
- -- If the page is not full, fill it out with blank ads.
- repeat while (size ads < self.adsPerPage) do append ads self.adProtocol
-
- ads
- )
-
- --Each time you change the page you want to make the pics purgeable
- method changePage self {class ClassifiedAds} page ->
- (
- local previousPageData, nextPageData
-
- previousPageData := self.pageData
- self.pageData := pageData self pageNum:self.cursor
-
- -- To be sure page updating occurs all at once
- if (self.presentedBy !== undefined) do
- self.presentedBy.compositor.enabled := false
- nextmethod self page
- if (self.presentedBy !== undefined) do
- self.presentedBy.compositor.enabled := true
-
- foreach previousPageData (item arg -> \
- if item <> arg.adProtocol do makepurgeable item.adpic) self
- )
-
- method updatePageButtons self {class ClassifiedAds} direction ->
- (
- -- check to make sure we don't page out of bounds
- if direction = @backward then
- self.cursor:= max 1 (self.cursor - 1)
- else
- self.cursor:= min (size self) (self.cursor + 1)
-
- -- if we are on the first page, disable the pageLeft button
- self.pageLeft.enabled := self.cursor > 1
-
- -- if we are on the last page, disable the pageRight button
- self.pageRight.enabled := self.cursor < (size self)
- undefined
- )
-
- -- Create a couple of convenience functions which take a Selection (the kind
- -- of result returned by a selection statement), and add or remove the ad objects
- -- to/from the classified ads.
- method postAds self {class ClassifiedAds} adList ->
- (
- for c in adList do addClassified self c
-
- -- Goto the first page
- goto self self.cursor
- forward self
- )
-
- method unPostAds self {class ClassifiedAds} adList ->
- (
- for c in adList do removeClassified self c
- goto self self.cursor
- )
-
- "Compiled ads.sx"
- -->>>
-